package eshin.blog.action;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import eshin.blog.beans.LoginInfo;
import eshin.blog.utils.LoginCheckUtil;


@Controller("loginCheckAction")
@Scope("singleton")
public class LoginCheckAction {

	private String username;
	private String password;
	private final static int MAX_RETRY_TIMES = 3;
	private final static double MAX_LOCK_HOURS = 0.1;
	private Map<String, LoginInfo>loginInfoCache=new ConcurrentHashMap<String, LoginInfo>();
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String execute() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		LoginInfo loginInfo=loginInfoCache.get(username);
		String loginMsg=null;
		if(null == loginInfo){
			loginInfo = new LoginInfo();
			loginInfo.setUsername(username);
			loginInfoCache.put(username, loginInfo);
		}else if(loginInfo.isLock()){
			double lockTime = loginInfo.getLockTime();
			double lockLast = LoginCheckUtil.checkLockTime(lockTime);
//	                    检查是否已过冻结时间
			if(lockLast>MAX_LOCK_HOURS){
				loginInfo.setLock(false);
				loginInfo.setRetryCount(0);
			}else{
				loginMsg=String.format("用户%s已被冻结，请%s小时后重试",username,MAX_LOCK_HOURS-lockLast);
			}
		}
		//用户名密码正确，且用户未被冻结时可登录
		if(LoginCheckUtil.checkUserPwd(username, password) && !loginInfo.isLock()){
			String preurl=request.getParameter("url");
			request.setAttribute("info", "登录成功");
			loginInfoCache.remove(username);
			System.out.println(preurl);
			if(preurl==null||preurl.equals(""))
			{
				request.setAttribute("url", "${pageContext.request.contextPath}/showUserAllArticle.action");
			}else{
				request.setAttribute("url", preurl);
			}
			return "login2";
		}else{
//			用户未超过重试次数
			if(!LoginCheckUtil.checkOverTry(MAX_RETRY_TIMES,loginInfo.getRetryCount())){
				int curCount = loginInfo.getRetryCount()+1;
				loginInfo.setRetryCount(curCount);
				loginMsg=String.format("还剩%s次机会重试", MAX_RETRY_TIMES-curCount);
//				当用户超过重试次数，并且并未冻结用户时
			}else if(!loginInfo.isLock()){
				loginInfo.setLock(true);
				loginInfo.setLockTime(new Date().getTime());
				loginMsg=String.format("用户%s已被冻结，请%s小时后重试",username,MAX_LOCK_HOURS);
//				当用户超过重试次数，并且已冻结用户时

			}
			
			request.setAttribute("url", "login2.jsp");
			request.setAttribute("info", "登录失败"+loginMsg);
			return "fail2";
		}
//		User user = new User();
//		BlogInfo blogInfo=blogInfoService.getBlogInfo(username);
//		user.setUsername(username);
//		user.setPassword(password);
//		//���request
//		HttpServletRequest request = ServletActionContext.getRequest();
//		if(userService.loginUser(user)) {
//			String preurl=request.getParameter("url");
//			request.setAttribute("info", "��½�ɹ�");
//			System.out.println(preurl);
//			if(preurl==null||preurl.equals(""))
//			{
//				request.setAttribute("url", "${pageContext.request.contextPath}/showUserAllArticle.action");
//			}else{
//				request.setAttribute("url", preurl);
//			}
//			
//			//��username���浽session��Χ��
////			HttpSession session =request.getSession();
//			Map session = ActionContext.getContext().getSession();
//			//��ֹ���ε�¼������һ���û���session��¼
//			request.getSession().removeAttribute("blogtitle");
//			request.getSession().removeAttribute("idiograph");
////			session.setAttribute("username", username);	
//			session.put("username", username);
//			if(blogInfo!=null){
//				session.put("blogtitle", blogInfo.getBlogtitle());
//				session.put("idiograph", blogInfo.getIdiograph());
////			}
//			
//			return "login";
//		} else {
//			request.setAttribute("url", "login.jsp");
//			request.setAttribute("info", "��½ʧ��");
//			return "fail";
//		}
	}

	/**
	 * @return the loginInfoCache
	 */
	public Map<String, LoginInfo> getLoginInfoCache() {
		return loginInfoCache;
	}

	/**
	 * @param loginInfoCache the loginInfoCache to set
	 */
	public void setLoginInfoCache(Map<String, LoginInfo> loginInfoCache) {
		this.loginInfoCache = loginInfoCache;
	}


}
